package chances.epg.config;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import chances.epg.commons.utils.PropertiesUtils;
import chances.epg.navigator.EpgConstants;

@Configuration
@PropertySource("/WEB-INF/conf/epg.properties")
@ComponentScan(basePackages = "chances.epg.session,chances.wechat.config")
public class BaseMvcConfig extends WebMvcConfigurationSupport {

	@Value("${epg.sites.path:/sites}")
	private String sitesRoot;

	@Value("${epg.resource.root:/resource}")
	private String resourcePath;

	@Value("${epg.search.api:/api/search/%s}")
	private String searchApiUrl;

	@Value("${epg.server.url}")
	private String epgHost;

	// @Bean
	// public AccessContextFactory accessContextFactory(){
	// return new AccessContextFactory();
	// }
	//
	// @Bean NavigateUrlFactory navigateUrlFactory(){
	// return new NavigateUrlFactory();
	// }
	//
	// @Bean TemplateConfigService templateConfigService(){
	// return new TemplateConfigService();
	// }
	//
	
	@Bean
	public InternalResourceViewResolver getInternalResourceViewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setViewClass(JstlView.class);
		resolver.setPrefix(sitesRoot);
		return resolver;
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		super.setServletContext(servletContext);
		servletContext.setAttribute(EpgConstants.EPG_CONTEXT_PATH, servletContext.getContextPath());
		servletContext.setAttribute(EpgConstants.EPG_RESOURCE_PATH, resourcePath);
		servletContext.setAttribute(EpgConstants.EPG_HOST, this.epgHost);

		File sitesPath = new File(servletContext.getRealPath(sitesRoot));
		File[] files = sitesPath.listFiles(new FileFilter() {

			@Override
			public boolean accept(File pathname) {
				return pathname.isDirectory();
			}
		});
		for (File file : files) {
			String name = file.getName();
			File sitefile = new File(file, name + ".properties");
			try {
				Map<String, Object> params = PropertiesUtils.loadConfig(sitefile);
				EpgSiteContext siteContext = new EpgSiteContext(name, params);
				EpgSiteContext.save(servletContext, siteContext);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public String getSitesRoot() {
		return sitesRoot;
	}

	public String getResourcePath() {
		return resourcePath;
	}

	public void setSitesRoot(String sitesRoot) {
		this.sitesRoot = sitesRoot;
	}

	public void setResourcePath(String resourcePath) {
		this.resourcePath = resourcePath;
	}

	public String getSearchApiUrl() {
		return searchApiUrl;
	}

	public void setSearchApiUrl(String searchApiUrl) {
		this.searchApiUrl = searchApiUrl;
	}

	public String getEpgHost() {
		return epgHost;
	}

	public void setEpgHost(String epgHost) {
		this.epgHost = epgHost;
	}

}
